//
//  SocketStream.m
//  SocketDemo
//
//  Created by bolaaw on 2018/4/11.
//  Copyright © 2018年 Koharu. All rights reserved.
//

#import "SocketStream.h"

@implementation SocketStream {
    
    NSInputStream *_inputStream;//输入流
    NSOutputStream *_outputStream;//输出流
}
    
    
+(instancetype)shareInstance{
    

    static SocketStream *streamInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        streamInstance = [[self alloc] init];
    });
    return streamInstance;
}
    
#pragma mark - 连接到主机
-(void)connectToHost{
    
    //设置流的配置信息
    NSString *host = @"127.0.0.1";
    uint32_t port = 5222;
    
    //定义C语言的输入输出流
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    
    CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)host, port, &readStream, &writeStream);
    
    //C语音输入输出流转换成OC输入输出流
    _inputStream = (__bridge NSInputStream *)(readStream);
    _outputStream = (__bridge NSOutputStream *)(writeStream);

    //设置输入输出流代理
    _inputStream.delegate = self;
    _outputStream.delegate = self;
    
    //把输入输入流添加到主运行循环
    [_inputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    [_outputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    
    //打开输入输出流
    [_inputStream open];
    [_outputStream open];
}
    
#pragma mark - Socket输入输出流代理
-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{
    
    switch (eventCode) {
        case NSStreamEventOpenCompleted:
        NSLog(@"aStream: %@",aStream);
        NSLog(@"成功连接建立，形成输入输出流的传输通道");
        break;
        
        case NSStreamEventHasBytesAvailable:
        NSLog(@"有数据可读");
        //读取数据
        [self readData];
        break;
        
        case NSStreamEventHasSpaceAvailable:
        NSLog(@"可以发送数据");
        break;
        
        case NSStreamEventErrorOccurred:
        NSLog(@"有错误发生，连接失败");
        break;
        
        case NSStreamEventEndEncountered:
        NSLog(@"正常的断开连接");
        [_inputStream close];
        [_outputStream close];
        [_inputStream removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
        [_outputStream removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
        break;
        
        default:
        break;
    }
}

    
#pragma mark - 读了服务器返回的数据

-(void)readData{
    
    //建立一个缓冲区 可以放1024个字节
    uint8_t buf[1024];
    //返回实际装的字节数
    NSInteger len = [_inputStream read:buf maxLength:sizeof(buf)];
    //把字节数组转化成字符串
    NSData *data =[NSData dataWithBytes:buf length:len];
    //从服务器接收到的数据
    NSString *reciveString =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    [self receiveMessage:reciveString];
    
}
    
#pragma mark - 接收数据
-(void)receiveMessage:(NSString *)message{
    NSLog(@"接收到消息啦：%@",message);
}
#pragma mark - 发送数据
-(void)sendMessage:(NSString *)message{
    
    NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];
    [_outputStream write:data.bytes maxLength:data.length];
}
    
    

@end
